package OliveOPML;

# Copyright (c) 2005, Shawn Boyette
# All rights reserved.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, provided
# that the above copyright notice(s) and this permission notice appear
# in all copies of the Software and that both the above copyright
# notice(s) and this permission notice appear in supporting
# documentation.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE
# COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR
# ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.
#
# Except as contained in this notice, the name of a copyright holder
# shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written
# authorization of the copyright holder.

require Exporter;
use warnings;
use strict;
use Config::YAML;
use DBI;
use Digest::MD5 qw( md5_hex );
use XML::Simple;

our @ISA       = qw(Exporter);
our @EXPORT    = qw( &opmlimport );

my $c = Config::YAML->new( config => "$ENV{HOME}/.olive/olive.yaml" );
my $i = my $j = 0;

sub opmlimport {
    my $opml = shift;

    my $x = XML::Simple->new;
    my $f = $x->XMLin($opml);

    print "Beginning OPML import.\n";

    foreach ( @{$f->{body}{outline}} ) {
        extract($_);
    }

    print "$j of $i feeds imported\n";
    print "See ~/.olive/errors.log for more information\n" if ($i != $j);
}

sub extract {
    my $outline = shift;

    my $text = my $url = '';

    $text = $outline->{text};

    if ($outline->{url}) {
        $url = $outline->{url};
    } elsif ($outline->{xmlUrl}) {
        $url = $outline->{xmlUrl};
    } else {
        foreach my $ol ( @{$outline->{outline}} ) {
            extract($ol);
        }
    }

    # throw back "container" outlines with no feed attached
    return unless ($outline->{text} &&
                   ($outline->{url} || $outline->{xmlUrl}));

    storefeed($text,$url);
}

sub storefeed {
    my ($disp,$feed) = @_;

    $i++;

    # trim whitespace
    $feed =~ s/^\s+//;
    $feed =~ s/\s+$//;
    $disp =~ s/^\s+//;
    $disp =~ s/\s+$//;
    $disp = substr($disp,0,16);

    # save original nick for display and sanitize
    my $nick = $disp;
    $nick =~ s/\s/_/g;
    $nick =~ s/\W//g;
    $nick =~ s/_+/_/g;
    $nick =~ s/_$//;
    $nick = md5_hex($disp) if ($nick eq '');
    $nick = lc($nick);
    
    # check for dupe nicks
    while (defined $c->{feeds}{$nick}) {
        print STDERR "The nick '$disp' is in use or reduces to a nick which is in use ($feed).\n";
        return;
    }        

    # check for dupe feeds
    foreach my $f (keys %{$c->{feeds}}) {
        if ( ($f ne $nick) && ($feed eq $c->{feeds}{$f}{feed}) ) {
            my $disp = $c->{feeds}{$f}{disp};
            print STDERR "You're already subscribed to $feed as '$disp'\n";
            return;
        }
    }

    # everything looks okay. store it.
    $c->{feeds}{$nick}{feed}    = $feed;
    $c->{feeds}{$nick}{disp}    = $disp;
    $c->{feeds}{$nick}{force}   = 0;
    $c->{feeds}{$nick}{dormant} = 0;
    $c->{feeds}{$nick}{last}    = 0;
    $c->{feeds}{$nick}{ttl}     = 0;
    $c->write;
    $j++;
}

1;
